package cn.fanyamin.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import cn.fanyamin.business.ToolService;
import cn.fanyamin.domain.UserCategory;
import cn.fanyamin.domain.Friend;
import cn.fanyamin.domain.User;

public class AddressAction extends SecureBaseAction {
	private static Log logger = LogFactory.getLog(AddressAction.class);

	public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
		ToolService toolService = getWebApp().getToolService();
		   
		
		User user = getUserFromSession(request);
        
		setCategories(request, UserCategory.CATEGORY_TYPE_FRIEND);

	    List<Friend> friendList = toolService.listFriend(user.getUserID());
	    request.setAttribute("friendList", friendList);
		logger.info("friendList: " + friendList);
		return new ActionForward("/WEB-INF/jsp/struts/tool/addressbook.jsp");
	}
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
	                            HttpServletRequest request, HttpServletResponse response) {
		ToolService toolService = getWebApp().getToolService();
		Friend friend = getFriendFromForm(form);
		if(friend == null) {
		    return mapping.findForward("failure");
		}
		User user = getUserFromSession(request);
		friend.setUserID(user.getUserID());
		int friendID = toolService.createFriend(friend);
		friend.setFriendID(friendID);
		logger.info("create friend: " + friend);
		return mapping.findForward("success");
		//return new ActionForward("/WEB-INF/jsp/struts/tool/addressbook.jsp");
	}
	
    private Friend getFriendFromForm(ActionForm form) {
		Friend friend = new Friend();
		LazyValidatorForm friendForm = (LazyValidatorForm) form;

		friend.setFriendID(NumberUtils.toInt((String) friendForm.get("friendID")));
		friend.setName((String) friendForm.get("name"));	
		friend.setEmail((String) friendForm.get("email"));	
		friend.setAddress((String) friendForm.get("address"));
		friend.setMobile((String)friendForm.get("mobile"));
		friend.setPhone((String)friendForm.get("phone"));
		friend.setIm((String)friendForm.get("im"));
		return friend;
	}

    public ActionForward retrieve(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm friendFrom = (LazyValidatorForm)form;
        int friendID = NumberUtils.toInt((String)friendFrom.get("friendID"));
        Friend friend = toolService.retrieveFriend(friendID);
        request.setAttribute("friend", friend);
        return new ActionForward("/WEB-INF/jsp/struts/tool/friend.jsp");
    }
    
        
    public ActionForward update(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        
        ToolService toolService = getWebApp().getToolService();
        Friend friend = getFriendFromForm(form);
        toolService.updateFriend(friend);
        //request.setAttribute("friendType", friend.getFriendType());
        logger.info("update friend, friend=" + friend);
        return mapping.findForward("success");
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm friendFrom = (LazyValidatorForm)form;
        int friendID = NumberUtils.toInt((String)friendFrom.get("friendID"));
        toolService.deleteFriend(friendID);
        int friendType = NumberUtils.toInt(request.getParameter("friendType"));
        //request.setAttribute("friendType", friendType);
        logger.info("delete friend, friendID=" + friendID);
        return mapping.findForward("success");
        
    }
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
    	logger.info("add friend");
    	
    	setCategories(request, UserCategory.CATEGORY_TYPE_FRIEND);
    	
    	return new ActionForward("/WEB-INF/jsp/struts/tool/friend.jsp");
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm friendFrom = (LazyValidatorForm)form;
        int friendID = NumberUtils.toInt((String)friendFrom.get("friendID"));
        Friend friend = toolService.retrieveFriend(friendID);
        request.setAttribute("friend", friend);
        
        setCategories(request, UserCategory.CATEGORY_TYPE_FRIEND);
        
        return new ActionForward("/WEB-INF/jsp/struts/tool/friend.jsp");
    }
}
